﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Water.Web.Controls.BaseInterface;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;

namespace Water.Web.Controls
{
    public class BoundField:System.Web.UI.WebControls.BoundField,IDataControlFieldEx
    {
        #region private fields

        private string headerPrpmpt;

        private string footerPrompt;

        private SummaryType summaryFunction = SummaryType.No;

        IList<double> values = new List<double>();

        private bool matrixColumn = false;

        #endregion


        #region properties

        [Browsable(true), GlobalizeCategory("PROMPT"), GlobalizeDescription("HeaderPrpmpt")]
        public virtual string HeaderPrompt
        {
            get { return headerPrpmpt; }
            set { headerPrpmpt = value; }
        }

        [Browsable(true), GlobalizeCategory("PROMPT"), GlobalizeDescription("FooterPrompt")]
        public virtual string FooterPrompt
        {
            get { return footerPrompt; }
            set { footerPrompt = value; }
        }

        public override string HeaderText
        {
            get
            {
                if(string.IsNullOrEmpty(base.HeaderText) && !string.IsNullOrEmpty(HeaderPrompt))
                    base.HeaderText = Water.Web.Prompt.PromptProxy.GetPromptDesc(HeaderPrompt);
                return base.HeaderText;
            }
            set
            {
                base.HeaderText = value;
            }
        }

        public override string FooterText
        {
            get
            {
                if (string.IsNullOrEmpty(base.FooterText) && !string.IsNullOrEmpty(FooterPrompt))
                    base.FooterText = Water.Web.Prompt.PromptProxy.GetPromptDesc(FooterPrompt);
                return base.FooterText;
            }
            set
            {
                base.FooterText = value;
            }
        }

        [Browsable(true), GlobalizeCategory("Behavior"), GlobalizeDescription("SummaryFunction"), DefaultValue(SummaryType.No)]
        public SummaryType SummaryFunction
        {
            get { return summaryFunction; }
            set { summaryFunction = value; }
        }

        public IList<double> Values
        {
            get { return values; }
        }

        [Browsable(true), GlobalizeCategory("Behavior"), GlobalizeDescription("MatrixColumn"), DefaultValue(false)]
        public bool MatrixColumn
        {
            get { return matrixColumn; }
            set { matrixColumn = value; }
        }


        protected override object GetValue(System.Web.UI.Control controlContainer)
        {
            object _val = base.GetValue(controlContainer);
            if (SummaryType.No != this.summaryFunction)
            {
                try
                {
                    values.Add(double.Parse(_val.ToString()));
                }
                catch
                { }
            }
            else
            {
                if (values.Count() > 0)
                    values.Clear();
            }
            return _val;
        }


        //protected override void OnDataBindField(object sender, EventArgs e)
        //{
        //    base.OnDataBindField(sender, e);

        //    if (SunmaryType.No != this.summaryFunction)
        //    {
        //        try
        //        {
        //            Control control = (Control)sender;
        //            Control namingContainer = control.NamingContainer;
        //            object dataValue = this.GetValue(namingContainer);
        //            values.Add(double.Parse(dataValue.ToString()));
        //        }
        //        catch
        //        { }
        //    }
        //    else
        //        values.Clear();

        //}
        public string GetFormatAggregateDataValue(Control control)
        {
            if (Values.Count() > 0 && SummaryType.No != this.SummaryFunction)
            {
                double _dateValue = 0;
                bool encode = (this.SupportsHtmlEncode && this.HtmlEncode) && (control is TableCell);

                switch (this.SummaryFunction)
                {
                    case SummaryType.Sum:
                        _dateValue = Values.Sum();
                        break;
                    case SummaryType.Average:
                        _dateValue = Values.Average();
                        break;
                    case SummaryType.Count:
                        _dateValue = Values.Count();
                        break;
                    case SummaryType.Max:
                        _dateValue = Values.Max();
                        break;
                    case SummaryType.Min:
                        _dateValue = Values.Min();
                        break;
                }
                return FormatDataValue(_dateValue, encode);
            }
            else
                return @"&nbsp;";

        }
       
         
        #endregion
    }
}
